<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="success" @click="handlequs('1')" v-auth="'btn:jieshou'">
          <Icon icon="ant-design:check-circle-outlined" /> {{ t('接收') }}
        </a-button>

        <a-button type="danger" @click="handlequs('2')" v-auth="'btn:bujieshou'">
          <Icon icon="ant-design:close-circle-outlined" /> {{ t('不接收') }}
        </a-button>

        <a-button type="primary" @click="handleForm({})" v-auth="'btn:add:shujubiangeng'">
          <Icon icon="fluent:add-12-filled" /> {{ t('申请数据变更') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handlexqForm({ id: record.id })">
          {{ record.content }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <resultForm @register="registerResultDrawer" @success="handleSuccess" />
    <xqform @register="registerxqDrawer" @success="handleSuccess" />

    <BasicModal v-bind="$attrs" @register="registerModal1" title="申请通过" @ok="handleModal('1')">
      <BasicForm @register="registerForm1">
        <template #msgContent="{ model, field }">
          <WangEditor v-model:value="model[field]" :bizType="'remarks_' + field" :height="500" />
        </template>
      </BasicForm>
    </BasicModal>
    <BasicModal
      width="60%"
      v-bind="$attrs"
      @register="registerModal2"
      title="申请不通过"
      @ok="handleModal('2')"
    >
      <BasicForm @register="registerForm2">
        <template #msgContent="{ model, field }">
          <!-- 
          :bizType="'remarks_' + field" -->
          <WangEditor v-model:value="model[field]" :bizType="'remarks_' + field" :height="500" />
        </template>
      </BasicForm>
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsOrtherQusPmQusrequestList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmQusrequestDelete,
    pmQusrequestListData,
    pmQusrequestPass,
    pmQusrequestBack,
    pmQusrequestResult,
  } from '/@/api/orther/qus/pmQusrequest';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import resultForm from './resultForm.vue';
  import xqform from './xqform.vue';
  import { WangEditor } from '/@/components/WangEditor';

  import { BasicModal, useModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';

  const { t } = useI18n('orther.qus.pmQusrequest');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('数据变更申请管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('紧急程度'),
        field: 'level',
        component: 'Select',
        componentProps: {
          dictType: 'zf_qus_level',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('问题描述'),
        field: 'content',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'zf_qus_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('紧急程度'),
      dataIndex: 'level',
      key: 'a.level',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'zf_qus_level',
    },
    {
      title: t('问题描述'),
      dataIndex: 'content',
      key: 'a.content',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('要求处理日期'),
      dataIndex: 'reqHandDate',
      key: 'a.req_hand_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('计划处理日期'),
      dataIndex: 'planDate',
      key: 'a.plan_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('处理方式'),
      dataIndex: 'handDevice',
      key: 'a.hand_device',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('问题反馈'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('处理结果'),
      dataIndex: 'result',
      key: 'a.result',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'zf_qus_result',
    },
    {
      title: t('实际处理日期'),
      dataIndex: 'handDate',
      key: 'a.hand_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('申请人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'zf_qus_status',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 140,
    align: 'left',
    actions: (record: Recordable) => [
      {
        icon: 'simple-line-icons:book-open',
        title: t('结果反馈'),
        onClick: handleResultForm.bind(this, { id: record.id }),
        auth: 'btn:jieguofankui',
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑数据变更申请'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'btn:edit:shujubiangeng',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除数据变更申请'),
        popConfirm: {
          title: t('是否确认删除数据变更申请'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'btn:delete:shujubiangeng',
        ifShow: () => record.status == '9',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerResultDrawer, { openDrawer: openResultDrawer }] = useDrawer();
  const [registerxqDrawer, { openDrawer: openxqDrawer }] = useDrawer();

  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: pmQusrequestListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  const [registerModal1, { openModal: openModal1, closeModal: closeModal1 }] = useModal();
  const [registerForm1, { resetFields: resetFields1, validate: validate1 }] = useForm({
    labelWidth: 120,
    schemas: [
      {
        label: t('计划处理日期'),
        field: 'planDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          // valueFormat: 'YYYY-MM-DD',
          showTime: false,
        },
        required: true,
      },
      {
        label: t('问题反馈'),
        field: 'remarks',
        component: 'InputTextArea',
        componentProps: {
          maxlength: 4000,
          rows: 8,
        },
        required: true,
        slot: 'msgContent',
        colProps: { lg: 24, md: 24 },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerModal2, { openModal: openModal2, closeModal: closeModal2 }] = useModal();
  const [registerForm2, { resetFields: resetFields2, validate: validate2 }] = useForm({
    labelWidth: 120,
    schemas: [
      {
        label: t('问题反馈'),
        field: 'remarks',
        component: 'InputTextArea',
        componentProps: {
          maxlength: 4000,
          rows: 8,
        },
        required: true,
        slot: 'msgContent',
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  function handlexqForm(record: Recordable) {
    openxqDrawer(true, record);
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  function handleResultForm(record: Recordable) {
    openResultDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmQusrequestDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
  let selIds = ref('');
  async function handlequs(e) {
    if (getSelectRows().length) {
      selIds.value = '';
      // let selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      if (e == '1') {
        resetFields1();
        openModal1(true, {});
      } else if (e == '2') {
        resetFields2();
        openModal2(true, {});
      }
      // const res = await pmMaterialCancelMain({ selIds: selIds.value });
      // showMessage(res.message);
      // handleSuccess();
    } else {
      showMessage('请先选择一条记录！');
    }
  }

  async function handleModal(e) {
    if (e == '1') {
      let data = await validate1();
      data.selIds = selIds.value;
      const res = await pmQusrequestPass(data);
      showMessage(res.message);
      handleSuccess();
      closeModal1();
    } else if (e == '2') {
      let data = await validate2();
      data.selIds = selIds.value;
      const res = await pmQusrequestBack(data);
      showMessage(res.message);
      handleSuccess();
      closeModal2();
    }
  }
</script>
